//
//  RegistSetpTwoViewController.swift
//  cxd4iphone
//
//  Created by hexy on 12/8/15.
//  Copyright © 2015 hexy. All rights reserved.
//

import UIKit

private struct kConstraints {
    
    static let headerHeight: CGFloat = xx_height(260)
    static let headerRect: CGRect    = CGRectMake(0, 0, xx_screenWidth(), kConstraints.headerHeight)
}

class RegistViewController: BaseTableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        

        tableView.tableHeaderView = registView
    }

    lazy var registView: RegistView = {
        
        let regist = RegistView(frame: kConstraints.headerRect)
        
        regist.agreeBtn.rac_signalForControlEvents(.TouchUpInside).subscribeNext({ (_) -> Void in
            
            let vc = WebViewController(title: "注册协议", URLStr: URL.agree)
            self.navigationController?.pushViewController(vc, animated: true)
            
        })
    
        regist.registBtn.rac_signalForControlEvents(.TouchUpInside).subscribeNext({ (_) -> Void in
            
            let userName = regist.userNameTextField.text!
            let passwd = regist.passwdTextField.text!
            let reffer = regist.refferPhoneTextField.text
            
            let mo = AccountViewModel.shared.registModel
            
            mo.userName = userName
            mo.password = passwd
            mo.referrer = reffer
            
            xx_showMaskHUD(nil)
            
            AccountViewModel.shared.regist().subscribeError({ (error) -> Void in
                
                    xx_showError(error)
                }, completed: { () -> Void in
                    
                    AccountViewModel.shared.login(userName, passwd: passwd).subscribeError({ (error) -> Void in
                        
                            xx_showError(error)
                        
                        }, completed: { () -> Void in
                            
                            xx_delay(1.2, task: { () -> () in
                                
                                xx_hideHUD()
                                xx_showSuccess("恭喜,注册成功!")
                                
                                xx_delay(1.2, task: { () -> () in
                                    
                                    AppDelegate.selectMeViewController()
                                    self.dismissViewControllerAnimated(true, completion: {})
                                })

                            })
                    })
            })
        })
        return regist
    }()

}
